package com.vivz.baby.persistence.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.vivz.baby.config.DefaultPersistenceJPAConfig;
import com.vivz.baby.dao.LogDAO;
import com.vivz.baby.domain.Log;

public class DataLoader {

    private LogDAO dao = null;

    public static void main(final String[] args) {

	final DataLoader dataLoader = new DataLoader();
	// dataLoader.deleteAll();
	dataLoader.load();
    }

    private void init() {
	@SuppressWarnings("resource")
	final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DefaultPersistenceJPAConfig.class);
	dao = context.getBean(LogDAO.class);
    }

    protected void delete() {

	init();
	dao.deleteAll();
    }

    protected void deleteAll() {

	init();

	final Calendar c = Calendar.getInstance();
	c.set(Calendar.YEAR, 2000);
	c.set(Calendar.MONTH, 1);
	c.set(Calendar.DATE, 1);

	final Date d1 = c.getTime();

	c.set(Calendar.YEAR, 2014);
	c.set(Calendar.MONTH, 1);
	c.set(Calendar.DATE, 1);
	final Date d2 = c.getTime();

	Long userId = null;
	dao.delete(dao.findByUserIdAndLogTimeBetween(userId, d1, d2));
    }

    public final void load() {

	init();

	final List<Integer> months = new ArrayList<Integer>();
	months.add(Calendar.JANUARY);
	months.add(Calendar.FEBRUARY);
	months.add(Calendar.MARCH);
	months.add(Calendar.APRIL);
	months.add(Calendar.MAY);
	months.add(Calendar.JUNE);
	months.add(Calendar.JULY);

	final List<Integer> dates = new ArrayList<Integer>();
	for (int i = 1; i < 29; i++)
	    dates.add(i);

	final Calendar c = Calendar.getInstance();
	c.set(Calendar.YEAR, 2014);

	final Collection<Log> logs = new ArrayList<Log>();

	for (final int month : months) {

	    c.set(Calendar.MONTH, month);

	    for (final int date : dates) {

		c.set(Calendar.DATE, date);

		for (int hr = c.getActualMinimum(Calendar.HOUR); hr <= c.getActualMaximum(Calendar.HOUR); hr++) {

		    c.set(Calendar.HOUR, hr);
		    c.set(Calendar.MINUTE, (getRandomInt() * 3) % 60);

		    // final Log log = new Log("LogServiceTest");
		    // log.setPumped(getRandomInt() * date % 2 == 0 ? 140 :
		    // 100);
		    // log.setFormula(getRandomInt() % 2 == 0 ? 120 : 60);
		    // log.setLeftBreast((getRandomInt() + date) % 2 == 0 ? 15 :
		    // 20);
		    // log.setRightBreast((getRandomInt() + 2 * date) % 2 == 0 ?
		    // 20 : 15);
		    //
		    // setDiaper(log, getRandomInt());
		    //
		    // log.setTimestamp(c.getTime());
		    // log.setUpdateTimestamp(new Date());
		    //
		    // logs.add(log);
		}
	    }
	}

	dao.save(logs);
    }

    private int getRandomInt() {
	int randomInt = (int) Double.doubleToLongBits(Math.random());
	while (randomInt < 1) {
	    randomInt *= 10;
	}
	return randomInt;
    }

    private void setDiaper(final Log log, final int i) {

	String d;

	switch (i % 4) {
	case 1:
	    d = "DT";
	    break;
	case 2:
	    d = "WD";
	    break;
	case 3:
	    d = "WT";
	    break;
	default:
	    d = "NO";
	    break;
	}

	// log.setDiaper(d);
    }

}
